package com.spzx.product.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: atguigu
 * @create: 2025-02-25 10:11
 */
@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        int i = Runtime.getRuntime().availableProcessors();
        int corePoolSize = i * 2;
        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(
                        corePoolSize,
                        corePoolSize,
                        0,
                        TimeUnit.SECONDS,
                        new ArrayBlockingQueue<>(300),
                        new ThreadPoolExecutor.CallerRunsPolicy()
                );
        threadPoolExecutor.prestartCoreThread();
        return threadPoolExecutor;
    }
}
